

function PPage(itemCount, pageSize) {
    this.itemCount = parseInt(itemCount);
    this.pageSize = parseInt(pageSize);
    this.curItem = 1;
    this.curPage = 1;

    if (itemCount % pageSize == 0) {
        this.pageCount = (itemCount / pageSize);
    } else {
        this.pageCount = parseInt((itemCount / pageSize)) + 1;
    }

}

PPage.prototype.forwardPage = function(i) {
    i = i < 1 ? 1 : i;
    this.curItem = this.pageSize * (i - 1) + 1;
    this.curPage = i;
}

PPage.prototype.getStart = function() {
    return this.curItem;
}

PPage.prototype.getEnd = function() {
    var i = this.curItem + this.pageSize - 1;
    i = i > this.itemCount ? this.itemCount : i;
    return i;
}
PPage.prototype.getPageCount = function(num) {
    var result = [];
    var before = parseInt(num / 2);
    var after = parseInt(num / 2);
    if (num % 2 == 0) {
        before--;
    } else {
        after++;
    }

    var cur = this.curPage;

    var start = cur - before;
    var end = cur + after;

    if (end >= this.pageCount && start <= 1) {
        end = this.pageCount;
        start = 1;
    } else if (end >= this.pageCount) {
        var cha = end - this.pageCount;
        start = start - cha;
        start = start < 1 ? 1 : start;

        end = this.pageCount;
    } else if (start <= 1) {
        var cha = 1 - start;
        end = end + cha;
        end = end > this.itemCount ? this.itemCount : end;

        start = 1;
    }

    for (var i = end; i >= start; i--) {
        result.push(i);
    }
    return result;
}

PPage.prototype.getPageInfo = function(i) {
    var s = this.pageSize * (i - 1) + 1;

    var e = s + this.pageSize - 1;
    e = e > this.itemCount ? this.itemCount : e;

    var obj = {
        start : s,
        end : e
    };
    return obj;
}